﻿using JV.Com.Business;
using JV.Com.Internals;
using JV.Com.Model;
using JV.Com.Tools;
using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Documents;

namespace JV.Com.ViewModels.Wrapper
{
    public sealed class NewsWrapper : ViewModel, IHasMainNavigationService
    {
        private readonly PageBaseViewModel m_parent;
        private readonly News m_news;

        public NewsWrapper(PageBaseViewModel parent, News news)
        {
            m_news = news;
            m_parent = parent;
        }

        private bool m_contentLoaded;
        public async Task LoadContentAsync()
        {
            if (!m_contentLoaded)
            {
                await DispatcherHelper.RunInUIThreadAsync(() => InLoading = true);

                await Service.FillNewsContentAsync(m_news);
                m_news.Content = m_news.Content.Insert(0, "<article>");
                m_news.Content = m_news.Content += "<article>";

                var converter = new Tools.HtmlToRichTextConverter(Tools.HtmlToRichTextConverter.ConvertType.News);
                m_inlines = converter.GetHTMLInlineCollection(m_news.Content);

                m_paragraph = m_inlines.ToParagraph();
                await DispatcherHelper.RunInUIThreadAsync(() => InLoading = false);
                await DispatcherHelper.RunInUIThreadAsync(RaiseContentPropertiesChanged);

                m_contentLoaded = true;
            }
        }

        public void RaiseContentPropertiesChanged()
        {
            OnPropertyChanged("FormatedDate");
            OnPropertyChanged("Paragraph");
            OnPropertyChanged("ImageUrl");
            OnPropertyChanged("Title");
            InLoading = false;
        }

        private bool m_inLoading;
        public bool InLoading { get { return m_inLoading; } set { SetProperty<bool>(ref m_inLoading, value); } }
        private Paragraph m_paragraph;
        public Paragraph Paragraph { get { return m_paragraph; } }
        private HTMLInlineCollection m_inlines;
        public HTMLInlineCollection Inlines { get { return m_inlines; } }
        public string FormatedDate { get { return string.Format("Le {0:d} à {1:d2}h{2:d2}", m_news.Date, m_news.Hour.Hours, m_news.Hour.Minutes); } }
        public int Id { get { return m_news.Id; } }
        public string Title { get { return m_news.Title; } }
        public string ImageUrl { get { return m_news.LargeImageUrl ?? m_news.SmallImageUrl; } }
        public string Summary { get { return m_news.Summary; } }
        public IEnumerable<byte[]> Machines
        {
            get
            {
                foreach (var machine in m_news.Machines)
                {
                    if (Cache.Instance.Machines.ContainsKey(machine))
                    {
                        var bytes = Cache.Instance.Machines[machine].Image;
                        yield return bytes;
                    }
                }
            }
        }
        public PageBaseViewModel Parent { get { return m_parent; } }
        public INavigationService NavigationService
        {
            get { return m_parent.NavigationService; }
        }
    }
}
